﻿/**
 *		TODO:如果知道一个类名, 能不能找到项目中的哪个FLA中某个库元件是链接该类的?
 *		tamt 	2009/08/18
 *		tamt	2009/08/20	ver 1.0
 */

//-------------界面文件的生成----------------------
var tmpXmlFile = fl.configURI + "/tmp.xml";
var xmlGui = buildXulGui();
FLfile.write(tmpXmlFile, xmlGui);
function buildXulGui()
{
	var t = fl.getDocumentDOM().pathURI;
	var folderPath = t.substr(0, t.lastIndexOf('/'));
  return '<dialog id="dialog" title="通过类名定位元件所在FLA" buttons="accept, cancel"> \
      \
      <script> \
      function browseFolder() \
      { \
        var folderURL = fl.browseForFolderURL(); \
        if(folderURL != null) \
        { \
          fl.xmlui.set("folder", folderURL); \
        } \
      } \
      </script> \
      \
     <vbox> \
      <label width="300" value="根据提供的类名(例如:htSprite), 在指定的文件夹下查看所有的FLA文件, 查找到库元件的链接名是指定类名(htSprite)的FLA文件, 并打开该FLA文件."/> \
      <separator/> \
      <label width="300" value="要查找的类名:" /> \
      <textbox width="300" value="ClassName" id="className"/> \
      <separator/> \
      <label value="文件夹:" /> \
      <textbox value="' + folderPath +'" width="300" id="folder"/> \
      <button id="openFolder" label="选择..." oncommand="browseFolder()" disabled="true"/>	\
    </vbox> \
    \
  </dialog>';
}
//-------------------------------------------------
var setting = fl.getDocumentDOM().xmlPanel(tmpXmlFile);

var className = setting.className;
var folder = setting.folder;

//得到文件夹下的所有fla文件
var flaFiles = [];

//列出文件夹下的所有fla文件
function listAllFolders(fdpath){
	var t = FLfile.listFolder(fdpath + '/' + '*.fla', 'files');
	for(var i=0; i<t.length; i++){
		t[i] = fdpath + '/' + t[i];
	}
	flaFiles = flaFiles.concat(t);
	var fds = FLfile.listFolder(fdpath, 'directories')
	for(var i=0; i<fds.length; i++){
		listAllFolders(fdpath+'/'+fds[i]);
	}
}
if(setting.dismiss=='accept'){
	listAllFolders(folder);
	//逐一查找FLA文件中的库是否有链接类名为className的元件.

	var keepOpenDocs = [];
	var keepOpenDoc;
	var ds = fl.documents;
	for(var i=0; i<ds.length; i++){
		var doc = ds[i];
		keepOpenDocs.push(doc.pathURI);
	}
	
	for(var i=0; i<flaFiles.length; i++){
		fl.openDocument(flaFiles[i]);
		keepOpenDoc = fl.getDocumentDOM();
		//搜索库中链接类名为className的元件.
		var items = fl.getDocumentDOM().library.items
		for(var j=0; j<items.length; j++){
			item = items[j];
			if(item.linkageExportForAS){
				if(item.linkageClassName.indexOf(className)>=0 || item.linkageBaseClass.indexOf(className)>=0){
					keepOpenDocs.push(flaFiles[i]);
					fl.trace(flaFiles[i]);
					//找到该元件后, 展开所在的文件夹, 选中该元件.
					if(item.name.lastIndexOf('/')>0){
						var pathNames = item.name.split('/');
						var path = '';
						for(var k=0; k<pathNames.length; k++){
							path += (k==0?'':'/') + pathNames[k];
							fl.getDocumentDOM().library.expandFolder(true, false, path);
						}
				
					}
					fl.getDocumentDOM().library.selectItem(item.name);
					
					break;
				}
			}
		}
	
		if(keepOpenDocs.indexOf(flaFiles[i])==-1){
			fl.closeDocument(fl.getDocumentDOM());
		}
	}
	
	if(keepOpenDoc)fl.setActiveWindow(keepOpenDoc);
}